package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;

import java.util.Set;

@Component
public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    /**
     * 每个月的最后一天凌晨2点钟执行一次清理
     */
    public void clearImg(){
        //比较上传到七牛云中的图片和保存到数据库中的图片的差异
        Set<String> sdiff = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        for (String img : sdiff) {

            //删除七牛云redis中的图片
            jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,img);
            //还要去删除七牛云中的多余的图片
            QiniuUtils.deleteFileFromQiniu(img);
        }
    }

}
